package com.orange.agent.todayNews.consumer;

import cn.hutool.core.util.StrUtil;
import com.orange.agent.todayNews.model.TodayNowCommonReq;
import com.orange.agent.todayNews.model.TodayNowSuperReq;
import com.orange.agent.todayNews.model.TodayNowSuperRes;
import com.orange.common.base.entity.ConsumerDto;
import com.orange.common.base.entity.RequestData;
import com.orange.common.base.entity.ResHeader;
import com.orange.common.enums.ResponseStatus;
import com.orange.common.utils.ConvertUtils;
import com.orange.framework.consumer.Consumer;
import com.orange.framework.consumer.annotation.HttpMapping;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeoutException;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2025/9/3 15:20
 */
@Slf4j
public abstract class AbstractTodayNewsConsumer implements Consumer {

    @Override
    public ConsumerDto communicate(HttpMapping mapping, Object serverBody, RequestData<?> requestData, Class returnType) throws TimeoutException {
        ConsumerDto consumerDto = new ConsumerDto();
        try {
            String response = "";
            String transCode = mapping.transCode();
            TodayNowCommonReq request = this.initParams(serverBody, requestData);
            //判断不是挡板
            if (isDefaultBody()) {
                log.info("挡板模式");
            } else {
                response = this.dataCommit(transCode, request);
                if (response == null) {
                    throw new TimeoutException(StrUtil.format("[{}]请求超时，请稍后再试"));
                }
            }

            TodayNowSuperRes todayNowDataRes = ConvertUtils.jsonToObject(response, TodayNowSuperRes.class);
            Object dataApiRes = ConvertUtils.jsonToObject(response, returnType);
            consumerDto.setServerReq(requestData.getBody());
            consumerDto.setServerRes(response);
            consumerDto.setBody(dataApiRes);
            consumerDto.setHeader(this.setHeader(todayNowDataRes));
            consumerDto.setStatus(this.status(todayNowDataRes));
        } catch (Exception e) {
            log.error("Today News 远程请求出错了:{}", e.getMessage());
        }
        return consumerDto;
    }

    private ResHeader setHeader(TodayNowSuperRes todayNowSuperRes) {
        ResHeader resHeader = new ResHeader();
        resHeader.setReturnCode(String.valueOf(todayNowSuperRes.getCode()));
        resHeader.setReturnMsg(todayNowSuperRes.getMessage());
        return resHeader;
    }

    private ResponseStatus status(TodayNowSuperRes todayNowSuperRes) {
        return todayNowSuperRes.isSuccess() ? ResponseStatus.SUCCESS : ResponseStatus.FAILD;
    }

    private TodayNowCommonReq initParams(Object serverBody, RequestData<?> requestData) {
        TodayNowCommonReq<TodayNowSuperReq> todayNowDataReq = new TodayNowCommonReq<>();
        TodayNowSuperReq superReq = (TodayNowSuperReq) serverBody;
        todayNowDataReq.setData(superReq);
        return todayNowDataReq;
    }

    protected boolean isDefaultBody() {
        return false;
    }

    protected abstract String dataCommit(String transCode, TodayNowCommonReq<TodayNowSuperReq> request);

}
